package org.example.product.controller;

import org.example.core.utils.R;
import org.example.product.entity.AttributeCategoryEntity;
import org.example.product.service.AttributeCategoryService;
import org.example.product.vo.RelateCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 小杰_cc
 * @version 1.0
 */
@RestController
@RequestMapping("/pms/backend/attrRelate")
public class AttributeCategoryController {
    @Autowired
    private AttributeCategoryService categoryAttributeService;

    /**
     * 根据属性id获取关联的分类
     */
    @GetMapping("/list/{attrId}")
    public R<List<RelateCategoryVo>> list(@PathVariable("attrId") Long attrId){
        List<RelateCategoryVo> vos = categoryAttributeService.queryCategoryListByAttrId(attrId);
        return R.ok(vos);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R<?> save(@RequestBody AttributeCategoryEntity categoryAttribute){
        return categoryAttributeService.saveCategoryAttr(categoryAttribute.getCategoryId(),categoryAttribute.getAttributeId())>0? R.ok():R.error();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R<?> delete(@PathVariable("id") Long id){
        return categoryAttributeService.deleteAttrCategoryById(id)>0? R.ok():R.error();
    }

}
